<?php
require_once 'CKEDITOR.php';

class News extends CI_Controller {
	
	function __construct() {
		parent::__construct ();
		$username = $this->session->userdata('username');
		$level = $this->session->userdata('level');
		if($username !='' && $level == 1 ){
			
		}
		else
		{
			redirect(base_url().'index.php/user');
		}
		$this->load->model ( 'be/NewsModel' );
		$this->load->helper ( 'form' );
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
		$this->load->helper ( 'ckeditor' );
	}
	
	public function index() {
		$data ['news'] = $this->NewsModel->getNews ();
		$data ['title'] = 'Tin tức CARDnet';
		$data['page_title'] = 'Danh sách bản tin';
		$data ['main_content'] = 'be/news/views';
		$this->load->view ( 'be/includes/template', $data );
	}
	
	public function create() {
		$data ['title'] = 'Đăng tin';
		$this->form_validation->set_rules ( 'title', 'Tiêu đề', 'required' );
		$this->form_validation->set_rules ( 'text', 'Nội dung' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['main_content'] = 'be/news/create';
			$data ['page_title'] = 'Đăng tin';
			$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
			$mess_detail .= '<p><a href="' . base_url () . 'index.php/news/add"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/news">Trở lại trang danh sách</a></p>';
			$data ['message_detail'] = $mess_detail;
		} else {
			$this->NewsModel->create ();
			$data['page_title'] = 'Đăng tin thành công';
			$mess_detail = '<p>Đăng tin thành công!</p>';
			$mess_detail .= '<p><a href="' . base_url () . 'index.php/news/add">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/news">Trở lại trang danh sách</a>';
			$data ['message_detail'] = $mess_detail;
			$data ['main_content'] = 'be/message';
		}
		$data['editor'] = CKEDITOR::createEditor();	
		$this->load->view ( 'be/includes/template', $data );
	}
	
	public function view($slug) {
		$data ['item'] = $this->NewsModel->getNews ( $slug );
		if (empty ( $data ['item'] )) {
			show_404 ();
		}
		$data ['title'] = $data ['item'] ['title'];
		$data ['news'] = $this->NewsModel->getNews ( $slug );
		$data ['main_content'] = 'be/news/item';
		$data['page_title'] = $data['title'];
		$this->load->view ( 'be/includes/template', $data );
	}
	
	function delete() {
		$ids = $this->input->post ( 'selected' );
		$flag = true;
		foreach ( $ids as $id ) {
			if (! $this->NewsModel->delete ( $id )) {
				$flag = false;
			}
		}
		if (! $flag) {
			//echo "Delete not successfully!";
		}
		redirect ( site_url ( '/news' ) );
	}
	
	function update($id) {
		
		$this->form_validation->set_rules ( 'title', 'Tiêu đề', 'required' );
		$this->form_validation->set_rules ( 'text', 'Nội dung' );
	
		
		if ($this->input->post('update')) {
			$data ['id'] = $id;
			$data ['title'] = $this->input->post ( 'title' );
			$data ['text'] = $this->input->post ( 'text' );
			$order = array("\r\n", "\n", "\r");
			$replace = "";
			$data['text'] = str_replace($order, $replace, $data['text']);
			
			$result = $this->NewsModel->update ( $id, $data );
			if ($result)
				redirect ( base_url () . 'index.php/news' );
			else
				echo "Can't update this card";
		} else {
			$news_item = $this->NewsModel->get( $id );
			$data ['title'] = $news_item->title;
			$data['text'] = $news_item->text;
			$data['id'] = $id;
			
			$data['main_content'] = 'be/news/update';					
			$this->load->view ( 'be/includes/template', $data );
		}
	}
}
